package com.party.controller.sys.excellentparty;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.ExcellentParty;
import com.party.model.SysUser;
import com.party.service.excellentparty.ExcellentPartyService;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/excellentParty")
public class ExcellentPartyController {

    @Resource
    ExcellentPartyService excellentPartyService;

    @Resource
    SysUserService sysUserService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/excellentparty/index");
        return modelAndView;
    }

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/excellentParty")
    public ModelAndView excellentParty() {
        ModelAndView modelAndView = new ModelAndView();
        List<ExcellentParty> selectListUser=excellentPartyService.getAllExcellentPartyByCustom();

        List<SysUser> listUser=sysUserService.getAllUsers();
        modelAndView.addObject("listUser",listUser);
        modelAndView.addObject("selectListUser",selectListUser);
        modelAndView.setViewName("/sys/excellentparty/excellentparty");
        return modelAndView;
    }

    /**
     * 添加优秀党员
     * @return
     */
    @RequestMapping(value = "/editExcellentParty")
    public ModelAndView editExcellentParty(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String msg="";

        String userids[] = request.getParameterValues("userid");
        int num=excellentPartyService.addExcellentPartyByUserIds(userids);
        if(num>0){
            msg="添加成功";
        }else{
            msg="添加失败";
        }

        modelAndView.addObject("msg",msg);
        modelAndView.setViewName("/sys/excellentparty/excellentparty");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String author = req.getParameter("author");
        String type = req.getParameter("type");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        ExcellentParty excellentParty = new ExcellentParty();
        excellentParty.setTitle(title);
        excellentParty.setAuthor(author);
        excellentParty.setType(type);
        Page p = this.excellentPartyService.getListPageByCustorm(excellentParty, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String author=request.getParameter("author");
        String msg="";
        ExcellentParty excellentParty=null;
        if(!"".equals(id)){
            int intId=Integer.parseInt(id);
            excellentParty=excellentPartyService.getExcellentPartyByPrimaryKey(intId);
        }
        excellentParty.setAuthor(author);
        if(excellentParty==null){
            msg="获取成功";
        }else{
            msg="获取失败";
        }

        byte[]binaryContent=excellentParty.getContent();
        String content="";
        if(binaryContent!=null){
            content=new String(excellentParty.getContent());
        }

        modelAndView.addObject("excellentparty",excellentParty);
        modelAndView.addObject("content",content);
        modelAndView.setViewName("/sys/excellentparty/edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
            @RequestParam(value = "id")Integer id,
            @RequestParam(value = "title")String title,
            @RequestParam(value = "author")String author,
            @RequestParam(value = "type")String type,
            @RequestParam(value = "content")String content,
            @RequestParam(value = "time")String time,
                                  HttpSession session,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        ExcellentParty excellentParty=new ExcellentParty();
        excellentParty.setContent(content.getBytes());
        String msg="";
        excellentParty.setId(id);
        excellentParty.setTitle(title);
        excellentParty.setAuthor(author);
        excellentParty.setType(type);
        excellentParty.setTime(time);

        excellentParty.setContent(content.getBytes());
        int num=excellentPartyService.updateExcellentPartyByPrimaryKey(excellentParty);
        if(num>0){
            msg="编辑成功";
        }else{
            msg="编辑失败";
        }

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);

        return map;
    }


    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = excellentPartyService.deleteExcellentPartyByIds(ids);

        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }
}
